<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
class omeanalysts_ctl_analysis_orderformreport extends desktop_controller{
    
    var $workground = 'omeanalysts_ctl_analysis_orderformreport';
    var $pagelimit = 10;
    public function __construct($app)
    {
        parent::__construct($app);
        header("cache-control: no-store, no-cache, must-revalidate");
    }

   function index()
   {
        $begin = strtotime($_POST['begin_date']);
        $end = strtotime($_POST['end_date']);
        $_POST['begin_date'] = $_POST['begin_date'] . ' ' . $_POST['_DTIME_']['H']['begin_date'] . ':' . $_POST['_DTIME_']['M']['begin_date'];
        $begin = strtotime($_POST['begin_date']);
        $_POST['end_date'] = $_POST['end_date'] . ' ' . $_POST['_DTIME_']['H']['end_date'] . ':' . $_POST['_DTIME_']['M']['end_date'];
        $end = strtotime($_POST['end_date']);
        $_POST['begin_date'] = $_POST['begin_date'] ? $_POST['begin_date'] : $begin_date;
        $_POST['end_date'] = $_POST['end_date'] ? $_POST['end_date'] : $end_date;
        if ($_POST['begin_date']) {
            $_POST['begin_date'] = strtotime($_POST['begin_date']);
            $_POST['begin_date'] = date("Y-m-d H:i:s", $_POST['begin_date']);
        }
        if ($_POST['end_date']) {
            $_POST['end_date'] = strtotime($_POST['end_date']);
            $_POST['end_date'] = date("Y-m-d H:i:s", $_POST['end_date']);
        }
    
        if(isset($_POST['orderform_bn']))
        {
            $orderform_bn=trim($_POST['orderform_bn']);
        }

        $_POST['orderform_bn'] = $_POST['orderform_bn'] ? $_POST['orderform_bn'] : $orderform_bn;

        $clearingtable['orderform_bn'] = $_POST['orderform_bn'];

        if(isset($_POST['pickinglist_bn']))
        {
            $pickinglist_bn=trim($_POST['pickinglist_bn']);
        }

        $_POST['pickinglist_bn'] = $_POST['pickinglist_bn'] ? $_POST['pickinglist_bn'] : $pickinglist_bn;

        $clearingtable['pickinglist_bn'] = $_POST['pickinglist_bn'];

        $deliverycompObj = app::get('wms')->model('deliverycomp');
        //获取快递名称
        $loginameList=$deliverycompObj->getLoginameList();
        $this->pagedata['loginameList'] = $loginameList;
        $clearingtable['logi_name'] = $_POST['logi_name'];
        //获取仓库名称
        $branchnameList=$deliverycompObj->getBranchnameList();
        $this->pagedata['branchnameList'] = $branchnameList;
        $clearingtable['name'] = $_POST['name'];

        #统计日期
        if ($_POST['begin_date'] == '1970-01-01 08:00:00' || !$_POST['begin_date']) {

            $begin_date = strtotime(date("Y-m-d H:i:s", time()));
            $clearingtable['begin_date'] = $begin_date;
        } else {
            $begin_date = $_POST['begin_date'];
            $clearingtable['begin_date'] = $begin;
        }
        if ($_POST['end_date'] == '1970-01-01 08:00:00' || !$_POST['begin_date']) {
            $end_date = strtotime(date("Y-m-d H:i:s", time()));
            $clearingtable['end_date'] = $end_date;
        } else {
            $end_date = $_POST['end_date'];
            $clearingtable['end_date'] = $end;
        }
        //$deliverycompObj = app::get('wms')->model('deliverycomp');
        $comp_data = $deliverycompObj->GetReportData($begin_date,$end_date,$orderform_bn,$pickinglist_bn,$_POST['logi_name'],$_POST['name']);
        $this->pagedata['comp_data'] = $comp_data;
       $this->pagedata['clearingtable'] = $clearingtable;
       // $logi_data = $deliverycompObj->GetReportLogi($begin_date,$end_date,$orderform_bn);
       // $this->pagedata['logi_data'] = $logi_data;
        
        $this->page('analysis/orderformreport/reportshow.html');
        
    }
  
    function report_order()
    {
        $begin_date = $_GET['begin_date'];
        $end_date = $_GET['end_date']; 
        //$begin_date = date("Y-m-d H:i:s", $begin_date);
        //$end_date = date("Y-m-d H:i:s", $end_date);

        $where = array('active' => 'true');
        if (!empty($this->orderform_bn)) {
            $orderform_bn = $this->orderform_bn;
            $where['orderform_bn'] = $this->orderform_bn;
        } else {
            $orderform_bn = "";
        }  
        if (!empty($this->pickinglist_bn)) {
            $pickinglist_bn = $this->pickinglist_bn;
            $where['pickinglist_bn'] = $this->pickinglist_bn;
        } else {
            $pickinglist_bn = "";
        }

        $_GET['orderform_bn'] = $_GET['orderform_bn'] ? $_GET['orderform_bn'] : $orderform_bn;
        $_GET['pickinglist_bn'] = $_GET['pickinglist_bn'] ? $_GET['pickinglist_bn'] : $pickinglist_bn;

        set_time_limit(600);
        $deliverycomp_obj = app::get('wms')->model('deliverycomp');
        $begin_date =strtotime($begin_date);//
        $end_date = strtotime($end_date);

        $comp_data = $deliverycomp_obj->GetOrderSumData_ordreformreport($begin_date,$end_date,$_GET['orderform_bn'],$_GET['pickinglist_bn'],$_GET['logi_name'],$_GET['name']);
        include_once ROOT_DIR . "/custom/lib/phpExcel.php";

        $excel = new phpExcel();
        $excel->addHeader(array(
            '客户名称', '料品', '快递', '仓库' ,'件数' , '体积汇总'
        ));
        if ($comp_data) {
            $data_xls = array();
            $qty_sum = $qty1_sum  = 0;

            foreach ($comp_data as $val) {
                $qty_sum += $val['qty'];
                $qty1_sum += $val['tovolume'];
 
                $data_xls[] = array(
                    $val['customername'],
                    $val['material_bn'],
                    $val['logi_name'],
                    $val['name'],
                    $val['qty'] ? $val['qty'] : 0,
                    $val['tovolume'] ? $val['tovolume'] : 0
                
                );
            }
        }
        $data_xls[] = array(
            '总数：',
            '',
            '',
            '',
            $qty_sum,
            $qty1_sum,
           
        );
        $excel->addBody(
            $data_xls
        );

        $excel->downLoad();
        exit;
    }



}
